﻿using Sc.Domain;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sc.Persistence
{
    public class BaseItemDAO
    {
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(BaseItem model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into BaseItem(");
            strSql.Append("Name,BaseItemTypeId,Remark)");
            strSql.Append(" values (");
            strSql.Append("@Name,@BaseItemTypeId,@Remark)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.NVarChar,50),
					new SqlParameter("@BaseItemTypeId", SqlDbType.Int,4),
					new SqlParameter("@Remark", SqlDbType.NVarChar,100)};
            parameters[0].Value = model.Name;
            parameters[1].Value = model.BaseItemTypeId;
            parameters[2].Value = model.Remark;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(BaseItem model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update BaseItem set ");
            strSql.Append("Name=@Name,");
            strSql.Append("BaseItemTypeId=@BaseItemTypeId,");
            strSql.Append("Remark=@Remark");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.NVarChar,50),
					new SqlParameter("@BaseItemTypeId", SqlDbType.Int,4),
					new SqlParameter("@Remark", SqlDbType.NVarChar,100),
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = model.Name;
            parameters[1].Value = model.BaseItemTypeId;
            parameters[2].Value = model.Remark;
            parameters[3].Value = model.Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// <param name="idlist">id字符串</param>
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from BaseItem ");
            strSql.Append(" where Id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 获取所有分类明细
        /// </summary>
        /// <returns></returns>
        public IList<BaseItem> GetList()
        {
            string sql = "SELECT * FROM BaseItem";
            IList<BaseItem> list = new List<BaseItem>();
            using (SqlDataReader dr = DbHelperSQL.ExecuteReader(sql))
            {
                while (dr.Read())
                {
                    try
                    {
                        BaseItem obj = new BaseItem();
                        obj.Id = (dr["Id"] == System.DBNull.Value ? 0 : dr.GetInt32(dr.GetOrdinal("Id")));
                        obj.Name = (dr["Name"] == System.DBNull.Value ? string.Empty : dr.GetString(dr.GetOrdinal("Name")));
                        obj.BaseItemTypeId = (dr["BaseItemTypeId"] == System.DBNull.Value ? 0 : dr.GetInt32(dr.GetOrdinal("BaseItemTypeId")));
                        obj.Remark = (dr["Remark"] == System.DBNull.Value ? string.Empty : dr.GetString(dr.GetOrdinal("Remark")));
                        list.Add(obj);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("从DataReader中读取数据失败，可能与相关的字段不存在有关！请检查！||" + ex.Message + "||" + ex.Source);
                    }
                }
                dr.Close();
            }
            return list;
        }
    }
}
